import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart' ;
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mfkb/config/Config.dart';
import 'package:mfkb/pages/modules/CustomRoute.dart';
import 'package:mfkb/pages/school/around/JobDetail.dart';
import 'package:mfkb/service/SchoolService.dart';
import 'package:toast/toast.dart';

class AroundJob extends StatefulWidget {
  dynamic data ;

  AroundJob(this.data) ;

  @override
  _AroundJobState createState() => _AroundJobState();
}

class _AroundJobState extends State<AroundJob> {

  ScrollController _controller = new ScrollController();
  var _mPage = 1;
  var flag = false ;

  @override
  void initState() {
    super.initState();
    if(widget.data["list"] == null || widget.data["list"].length == 0){
      widget.data["list"] = [] ;
      widget.data["page"] = 1 ;
      widget.data["mPage"] = 1 ;
      getData();
    }
    //给_controller添加监听
    _controller.addListener((){
      //判断是否滑动到了页面的最底部
      if(_controller.position.pixels == _controller.position.maxScrollExtent){
        //如果不是最后一页数据，则生成新的数据添加到list里面
        if(widget.data["mPage"] < widget.data["page"]){
          _retrieveData();
        }
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.dark ,
      child: Container(
        child: flag && (widget.data["list"] == null || widget.data["list"].length == 0) ? Center(child: Text("没有数据哦"),) : RefreshIndicator(
          onRefresh: _onRefresh,
          child: new ListView.separated(
            padding: EdgeInsets.all(0),
            controller: _controller,
            physics: BouncingScrollPhysics(),
            itemBuilder: (context,index){
              //判断是否构建到了最后一条item
              if(index == widget.data["list"].length && widget.data["page"] != 1){
                //判断是不是最后一页
                if(widget.data["mPage"] < widget.data["page"]){
                  //不是最后一页，返回一个loading窗
                  return new Container(
                    padding: EdgeInsets.all(16.0),
                    alignment: Alignment.center,
                    child: SizedBox(
                      width: 24.0,
                      height: 24.0,
                      child: CircularProgressIndicator(strokeWidth: 2.0,),
                    ),
                  );
                }else{
                  //是最后一页，显示我是有底线的
                  return new Container(
                    padding: EdgeInsets.all(16.0),
                    alignment: Alignment.center,
                    child: new Text('我是有底线的!!!',style:TextStyle(color: Colors.blue),),
                  );
                }
              }else{
                return GestureDetector(
                  child: Container(
                    margin: EdgeInsets.only(left:ScreenUtil().setWidth(15) , right:ScreenUtil().setWidth(15) , top:index == 0 ? ScreenUtil().setHeight(7) : 0),
                    height: ScreenUtil().setHeight(104),
                    decoration: BoxDecoration(
                      color: Colors.white ,
                      borderRadius: BorderRadius.all(Radius.circular(12)) ,
                    ) ,
                    child: Row(
                      children: <Widget>[
                        Container(
                          width: ScreenUtil().setHeight(100),
                          height: ScreenUtil().setHeight(80),
                          margin: EdgeInsets.only(left:ScreenUtil().setWidth(12)),
                          child: ClipRRect(
                            borderRadius: BorderRadius.all(Radius.circular(4)),
                            child: CachedNetworkImage(
                              imageUrl: Config().getImgHost()+widget.data["list"][index]["cover"] ,
                              fit: BoxFit.fill ,
                              placeholder: (context , url){
                                return (
                                  Container(
                                    color: Color.fromARGB(255, 246, 245, 248),
                                    child: Center(
                                      child: Image.asset("assets/images/imageloading.png"),
                                    ),
                                  )
                                ) ;
                              },
                            ),
                          ),
                        ) ,
                        Expanded(
                          child: Container(
                            height: ScreenUtil().setHeight(104),
                            margin: EdgeInsets.only(left:ScreenUtil().setWidth(12)),
                            padding: EdgeInsets.all(0),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start ,
                              children: <Widget>[
                                Container(
                                  height: ScreenUtil().setHeight(20),
                                  margin: EdgeInsets.only(top:ScreenUtil().setHeight(12)),
                                  child: Text(widget.data["list"][index]["name"] , overflow: TextOverflow.ellipsis,style: TextStyle(
                                    fontSize: ScreenUtil().setSp(16) ,
                                    fontWeight: FontWeight.bold ,
                                    height: ScreenUtil().setHeight(20) / ScreenUtil().setSp(16) ,
                                    color:Color.fromARGB(255, 19, 28, 53) ,
                                    fontFamily: "PingFang-SC-Bold,PingFang-SC"
                                  ),),
                                ) ,
                                Container(
                                  height: ScreenUtil().setHeight(14),
                                  margin: EdgeInsets.only(top:ScreenUtil().setHeight(10)),
                                  child: Row(
                                    children: <Widget>[
                                      Container(
                                        child: Text(widget.data["list"][index]["city"] , style: TextStyle(
                                          fontSize: ScreenUtil().setSp(12) ,
                                          fontWeight: FontWeight.w500 ,
                                          color: Color.fromARGB(255, 121, 132, 155) ,
                                          height: ScreenUtil().setHeight(14) / ScreenUtil().setSp(12)
                                        ),),
                                      ) ,
                                      Container(
                                        height: ScreenUtil().setHeight(10),
                                        width: ScreenUtil().setWidth(1),
                                        margin: EdgeInsets.only(left:ScreenUtil().setWidth(10) , right:ScreenUtil().setWidth(10)),
                                        color: Color.fromARGB(255, 236, 237, 241),
                                      ) ,
                                      Container(
                                        child: Text(widget.data["list"][index]["job"] , style: TextStyle(
                                          fontSize: ScreenUtil().setSp(12) ,
                                          fontWeight: FontWeight.w500 ,
                                          color: Color.fromARGB(255, 121, 132, 155) ,
                                          height: ScreenUtil().setHeight(14) / ScreenUtil().setSp(12)
                                        ),),
                                      ) ,
                                      Container(
                                        height: ScreenUtil().setHeight(10),
                                        width: ScreenUtil().setWidth(1),
                                        margin: EdgeInsets.only(left:ScreenUtil().setWidth(10) , right:ScreenUtil().setWidth(10)),
                                        color: Color.fromARGB(255, 236, 237, 241),
                                      ) ,
                                      Container(
                                        child: Text(widget.data["list"][index]["money_type"] == 0 ? "日结" : widget.data["list"][index]["money_type"] == 1 ? "月结" : "面谈" , style: TextStyle(
                                          fontSize: ScreenUtil().setSp(12) ,
                                          fontWeight: FontWeight.w500 ,
                                          color: Color.fromARGB(255, 121, 132, 155) ,
                                          height: ScreenUtil().setHeight(14) / ScreenUtil().setSp(12)
                                        ),),
                                      ) ,
                                    ],
                                  )
                                ) ,
                                Container(
                                  width: ScreenUtil().setWidth(60),
                                  height: ScreenUtil().setHeight(20),
                                  margin: EdgeInsets.only(bottom:ScreenUtil().setWidth(12) , top:ScreenUtil().setHeight(14)),
                                  child: Row(
                                    children: <Widget>[
                                      Text(widget.data["list"][index]["money"].toString() , style: TextStyle(
                                        color:Colors.red ,
                                        fontWeight: FontWeight.bold ,
                                        fontSize: ScreenUtil().setSp(18),
                                        height: ScreenUtil().setHeight(20) / ScreenUtil().setSp(18)
                                      ),) ,
                                      Text(widget.data["list"][index]["dw"] == 0 ? "元/日" : "元/月" , style: TextStyle(
                                        color:Colors.red ,
                                        fontWeight: FontWeight.bold ,
                                        fontSize: ScreenUtil().setSp(10),
                                        height: ScreenUtil().setHeight(20) / ScreenUtil().setSp(10)
                                      ),)
                                    ],
                                  ),
                                )
                              ],
                            ),
                          ),
                        ) ,
                        // Container(
                        //   width: ScreenUtil().setWidth(60),
                        //   height: ScreenUtil().setHeight(20),
                        //   margin: EdgeInsets.only(right:ScreenUtil().setWidth(12) , bottom:ScreenUtil().setWidth(12)),
                        //   child: Row(
                        //     children: <Widget>[
                        //       Text(widget.data["list"][index]["money"].toString() , style: TextStyle(
                        //         color:Colors.red ,
                        //         fontWeight: FontWeight.bold ,
                        //         fontSize: ScreenUtil().setSp(18),
                        //         height: ScreenUtil().setHeight(20) / ScreenUtil().setSp(18)
                        //       ),) ,
                        //       Text(widget.data["list"][index]["dw"] == 0 ? "元/日" : "元/月" , style: TextStyle(
                        //         color:Colors.red ,
                        //         fontWeight: FontWeight.bold ,
                        //         fontSize: ScreenUtil().setSp(10),
                        //         height: ScreenUtil().setHeight(20) / ScreenUtil().setSp(10)
                        //       ),)
                        //     ],
                        //   ),
                        // )
                      ],
                      crossAxisAlignment: CrossAxisAlignment.center ,
                    ),
                  ) ,
                  onTap: (){
                    Navigator.of(context).push(CustomRoute(JobDetail(id:widget.data["list"][index]["id"]))) ;
                  },
                );
              }
            },
            //分割线构造器
            separatorBuilder: (context,index){
              return Container(
                height: ScreenUtil().setHeight(8),
              );
            },
            //_items.length + 1是为了给最后一行的加载loading留出位置
            itemCount: widget.data["page"] == 1 ? widget.data["list"].length : widget.data["list"].length + 1
          ),
        )
      ),
    ) ;
  }

  void getData() {
    //初始数据源
    widget.data["mPage"] = 1 ;
    SchoolService().aroundJobs(widget.data["mPage"]).then((data){
      print(data) ;
      if(data["code"] == 200){
        widget.data["mPage"] = data["data"]["current"] ;
        widget.data["page"] = data["data"]["pages"] ;
        widget.data["list"] = data["data"]["list"] ;
        setState(() {
          flag = true ;
        });
      }else{
        Toast.show(data["msg"], context) ;
      }
    }) ;
  }

  void _retrieveData() {
    widget.data["mPage"]++ ;
    SchoolService().aroundJobs(widget.data["mPage"]).then((data){
      if(data["code"] == 200){
        widget.data["mPage"] = data["data"]["current"] ;
        widget.data["page"] = data["data"]["pages"] ;
        var temp = data["data"]["list"] ;
        for (int i=0;i<temp.length;i++){
          widget.data["list"].insert(widget.data["list"].length, temp[i]);
        }
        setState(() {
          
        });
      }else{
        Toast.show(data["msg"], context) ;
      }
    }) ;
  }

  // 下拉刷新
  Future<void> _onRefresh() async {
    await Future.delayed(Duration(milliseconds: 500) , (){
      getData() ;
    }) ;
  }

  @override
  void dispose() {
    //移除监听，防止内存泄漏
    _controller.dispose();
    super.dispose();
  }
}